// MyGraphInfoView.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "waveeditorDoc.h"
#include "MyGraphInfoView.h"
#include "MyGraphFrame.h"
#include "MyGraphView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MyGraphInfoView

IMPLEMENT_DYNCREATE(GraphInfoView, CScrollView)

GraphInfoView::GraphInfoView()
{
}

GraphInfoView::~GraphInfoView()
{
}


BEGIN_MESSAGE_MAP(GraphInfoView, CScrollView)
	//{{AFX_MSG_MAP(MyGraphInfoView)
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyGraphInfoView drawing

void GraphInfoView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 50;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

void GraphInfoView::OnDraw(CDC* pDC)
{
	ZWaveeditorDoc* pDoc = GetDocument();
	CRect winrect;
	GetClientRect(&winrect);
	wmaxx	=	winrect.right;
	// TODO: add draw code here
	int SelectedScenario = ((ChildGraphFrame*)GetParentFrame())->SelectedScenario;
	__int64	dx1		=	pDoc->_script[SelectedScenario]->mygraphdata.GetTotalSize();
	__int64	x2		=	pDoc->_script[SelectedScenario]->mygraphdata.GetOffset();
	__int64	dx2		=	pDoc->_script[SelectedScenario]->mygraphdata.GetPageLength();
	__int64	ratio	=	dx1/wmaxx;
	int		xx1		=	(long)(x2/ratio);
	int		xx2		=	xx1+(long)(dx2/ratio);
	DrawSign	(pDC,xx1,0,xx2+1,50,RGB(0,0,255));
	DrawGraphic	(pDC,0,5 ,wmaxx,15,RGB(128,128,128));
	DrawGraphic	(pDC,0,20,wmaxx,30,RGB(128,128,128));
	DrawGraphic	(pDC,0,35,wmaxx,45,RGB(128,128,128));
}

/////////////////////////////////////////////////////////////////////////////
// MyGraphInfoView diagnostics

#ifdef _DEBUG
void GraphInfoView::AssertValid() const
{
	CScrollView::AssertValid();
}

void GraphInfoView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

ZWaveeditorDoc* GraphInfoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ZWaveeditorDoc)));
	return (ZWaveeditorDoc*)m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// MyGraphInfoView message handlers

void GraphInfoView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	UpdateView();
}

void GraphInfoView::UpdateView()
{
	Invalidate();
	ChangeWindowTitle();
}

void GraphInfoView::ChangeWindowTitle()
{
	CFrameWnd* pFrame	=	GetParentFrame();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CString	name	=	"Graph View (";
	name			+=	pDoc->GetTitle()+")" + " - "+pDoc->GroupName;
	pFrame->SetWindowText(name);

}

void GraphInfoView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	ChangeWindowTitle();
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void GraphInfoView::OnKillFocus(CWnd* pNewWnd) 
{
	CScrollView::OnKillFocus(pNewWnd);
	ChangeWindowTitle();
	// TODO: Add your message handler code here
}

void GraphInfoView::DrawGraphic(CDC *pDC,int xst, int yst, int xend, int yend, COLORREF clr)
{
	if(xst	>	xend)
	{
		int tmp	=	xst ;
		xst		=	xend;
		xend	=	tmp;
	}
	if(yst	>	yend)
	{
		int tmp	=	yst ;
		yst		=	yend;
		yend	=	tmp;
	}
	int		delta		=	4;
	int		numofdeltas	=	abs(xend-xst)/(delta*2)+1;
	CPen	clpen(PS_SOLID,1,clr);
	CPen	*oldpen	=	pDC->SelectObject(&clpen);
	CPoint	lp[5];
	lp[0].y	=	lp[1].y	=	lp[4].y	=	yst;
	lp[2].y	=	lp[3].y	=	yend;
	lp[0].x	=	xst;
	lp[1].x	=	lp[2].x	=	xst+delta;
	lp[3].x	=	lp[4].x	=	xst+delta*2;
    for(int i=0;i<=numofdeltas;i++)
	{
		pDC->Polyline(lp,5);
		for(int ii=0;ii<5;ii++)
		{
			lp[ii].x	+= delta*2;
		}
	}
	pDC->SelectObject(oldpen);
	clpen.DeleteObject();
}
void GraphInfoView::DrawSign(CDC *pDC,int xst, int yst, int xend, int yend, COLORREF clr)
{
	if(xst	>	xend)
	{
		int tmp	=	xst ;
		xst		=	xend;
		xend	=	tmp;
	}
	if(yst	>	yend)
	{
		int tmp	=	yst ;
		yst		=	yend;
		yend	=	tmp;
	}
	CPen	clpen(PS_SOLID,1,clr);
	CBrush	clbrs(clr);
	CPen	*oldpen	=	pDC->SelectObject(&clpen);
	CBrush	*oldbrs	=	pDC->SelectObject(&clbrs);
	pDC->Rectangle(xst,yst,xend,yend);
	pDC->SelectObject(oldbrs);
	pDC->SelectObject(oldpen);
	clpen.DeleteObject();
	clbrs.DeleteObject();
}
